
//globals
var camera, scene, renderer;
var clock, controls;
var stats;
var xLight, yLight, zLight;
var lights = {};
var teapot;
var text;

//test some stuff here...
function fill_scene() {    
    var teapot_size = 400;
    var ambientLight = new THREE.AmbientLight(0x333333); // 0.2
    var teapot_color = new THREE.Color().setRGB(1.0, 0.8, 0.6);

    zLight = new THREE.DirectionalLight(0xffffff,1.0);
    zLight.position.set(320,390,700);

    scene.fog = new THREE.Fog(0xaaaaaa,2000,4000);

    scene.add(ambientLight);
    scene.add(zLight);

    //after doll.fetch using xhr to get the file a material is made
    //using the vs and fs
    doll.toon_shader.on_load = function() {
        //get a copy of the toom shader material
        teapot_shader = doll.toon_shader.copy();
        teapot = new THREE.Mesh(
            new THREE.TeapotGeometry(teapot_size, 20, true, true, true, true),
            teapot_shader);

        //configure shader
        teapot_shader.uniforms.uMaterialColor.value.copy(teapot_color);
	//teapot_shader.side = THREE.DoubleSide;
        teapot_shader.uniforms.uDirLightPos.value = zLight.position;
        teapot_shader.uniforms.uDirLightColor.value = zLight.color;
        scene.add(teapot);        
    }

    var s = "teapot.";
    text = doll.text.print(s,0,0,300);
    text.position.set(0,450,0);
    scene.add(text);
    
    //use xhr to get the toon shader
    doll.toon_shader.fetch(false);
}


function add_stats() {
    stats = new Stats();
    stats.setMode(0); // 0: fps, 1: ms

    // Align top-left
    stats.domElement.style.position = 'absolute';
    stats.domElement.style.left = '0px';
    stats.domElement.style.top = '0px';
    document.body.appendChild( stats.domElement );
}

var shader;
function init() {
    clock = new THREE.Clock();
    scene = new THREE.Scene();
    //first 75 or 45?
    //last 1000 or 80000?
    camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 80000 );
    camera.position.set(-600, 500, 1300);
    //camera.position.set(0, 0, 2000);
    renderer = new THREE.WebGLRenderer( {antialias: true} );
    //renderer = new THREE.WebGLRenderer();

    renderer.gammaInput = true;
    renderer.gammaOutput = true;
    renderer.setClearColor(0xaaaaaa,1.0);
    renderer.setSize( window.innerWidth, window.innerHeight );
    //controls = new THREE.TrackballControls(camera);

    //init engine
    doll.ui.init();
    doll.text.init();
    doll.fetch.init();
    doll.audio.init();
    doll.camera = camera;
    doll.scene = scene;
    doll.window_width = window.innerWidth;
    doll.window_height = window.innerHeight;

    document.body.appendChild( renderer.domElement );
}

function setup_audio() {
    //var bg =  'assets/audio/kpp_revolution.ogg';
    //var bg =  'assets/audio/oniichan_09.ogg';
    //var bg =  'assets/audio/galaxy/02.wav';
    var audio = [
        'ponponpon.wav',
        'oniichan_09.ogg'
    ];
    doll.audio.complete = function() {
        console.log('hey I got all my audio');
        //doll.audio.play(1);
    }
    //doll.audio.fetch(audio, false);
}

function animate() {
    requestAnimationFrame(animate, renderer.domElement);
    render();
}

function render() {
    setInterval( function () {
        console.profile('main loop');
        stats.begin();
    
        var delta = clock.getDelta();

        //teapot.rotation.y += 1.0 *delta;
        //teapot.rotation.x += 1.0 *delta;

        if(text) {
            //text.rotation.x += 1.0 * delta;
            //text.rotation.y += 1.0 * delta;
        }

        //doll.ui.animate(delta);
        renderer.render(scene, camera);
        stats.end();
        console.profileEnd('main loop');
    }, 1000/60);
}

init();
setup_audio();
fill_scene();
add_stats();
animate();
